<template>
  <div class="popup">
    <van-search
      v-model="searchValue"
      show-action
      :placeholder="iptPlaceholder"
      @search="onSearch"
      @cancel="onCancel"
      @input="searchIptNow"
    />
    <history
      :historyKeyWord="historyKeywordList"
      :hotKeyword="hotKeywordList"
      v-if="show == 1"
      @tagClick="tagClick"
    ></history>
    <my-list
      v-else-if="show == 3"
      :arr="ListArr"
      @cellClick="tagClick"
    ></my-list>
    <my-product
      v-else
      :filterCategory="filterCategory"
      :goodsList="goodsList"
      @valChange="priceChange"
      @categoryChange="categoryChange"
    ></my-product>
  </div>
</template>
 
<script>
import history from "@/components/home/history.vue";
import myProduct from "@/components/product/myProduct.vue";
import myList from "@/components/home/myList.vue";
import { getPopUpData, getSearchData, getSearchNowhData } from "@/request/api";
export default {
  name: "",
  components: {
    history,
    myProduct,
    myList,
  },
  data() {
    return {
      //暂时控制显示产品区块还是搜索区块 1是历史区块，2是产品区块,
      show: 1,
      iptPlaceholder: "",
      searchValue: "",
      //历史搜素记录数组
      historyKeywordList: [],
      // 热门搜索数组
      hotKeywordList: [],
      //传递给myproduct组件的商品分类
      filterCategory: [],
      //传递给myproduct组件的商品列表
      goodsList: [],
      //排列顺序，desc表示价格由高到低，asc表示价格由低到高
      order: "desc",
      //分类的id
      categoryId: 0,
      //排序的方式，id是默认的排序方式,还有price排序方式
      sort: "id",

      //实时搜索数据的数组
      ListArr: [],
    };
  },
  methods: {
    //子组件传过来的事件，用于修改下拉菜单选项时按照方式重新获取数据
    //价格选项改变时触发
    priceChange(val) {
      // console.log(val);
      this.order = val;
      this.sort = "price";
      this.onSearch();
    },

    //分类选项改变时触发
    categoryChange(val) {
      // console.log(val);
      //修改categoryid
      this.categoryId = val;
      // 重新请求数据
      this.onSearch();
    },
    //点击搜索或者按下回车键触发 搜索
    onSearch() {
      let _this = this;
      getSearchData({
        keyword: this.searchValue,
        page: 1,
        size: 20,
        order: _this.order,
        categoryId: _this.categoryId,
        sort: _this.sort,
      }).then((res) => {
        // console.log(res);
        if (res.errno == 0) {
          //跳转到产品区块
          _this.show = 2;
          //将请求到的数据解构出来，
          let { filterCategory, goodsList } = res.data;

          //对 分类数据的属性名进行修改I filterCategory，使用JSON.parse(JSON.stringify(filterCategory))进行深拷贝，然后修改他的属性名
          // replace(旧的,'新的')
          let arr = JSON.parse(
            JSON.stringify(filterCategory)
              .replace(/name/g, "text")
              .replace(/id/g, "value")
          );
          _this.goodsList = goodsList;
          _this.filterCategory = arr;
          console.log(_this.filterCategory);
        }
      });
    },
    onCancel() {
      //按下取消键触发该事件，返回上一个页面
      this.$router.go(-1);
    },

    //实时搜索功能输入框变化时触发
    searchIptNow() {
      this.show = 3;
      getSearchNowhData({
        keyword: this.searchValue,
      }).then((res) => {
        if (res.errno == 0) {
          // console.log(res.data);
          this.ListArr = res.data;
        }
      });
    },
    tagClick(val) {
      // console.log(val);
      this.searchValue = val;
      this.onSearch();
    },
  },
  created() {
    getPopUpData().then((res) => {
      // console.log(res);
      if (res.errno == 0) {
        this.historyKeywordList = res.data.historyKeywordList;
        this.iptPlaceholder = res.data.defaultKeyword.keyword;
        this.hotKeywordList = res.data.hotKeywordList;
      }
    });
  },
};
</script>
 
<style lang="less" scoped>
.popup {
  width: 100%;
  height: 100%;
  background: #efefef;
  // background: #fff;
  position: absolute;
  top: 0;
  z-index: 999;
}
</style>